
<template>
  <div >

    <el-dialog v-dialogDrag :close-on-click-modal="true" title="指标类别" :visible.sync="open2" width="800px" append-to-body>
    <div class="">
      <div class="common_button flex_ac">
        <div class="right_button margin_a flex">
          <a href="javascript:;" class="color_blue" @click="handleUpdate"><img class="btn_img" src="@/assets/pinggu/add.png" alt="">新增</a>
        </div>
      </div>
      <el-table v-loading="loading" :data="list">
        <el-table-column label="ID" align="center" width="80" prop="id"  />
        <el-table-column label="指标标题" align="center" prop="title"  />

        <el-table-column label="是否评委指标" align="center" prop="status">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.is_pw" :active-value="1" :inactive-value="0" @change="sw_change($event,scope.row)" ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="排序" align="center" prop="sort"  />

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160px" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleUpdate(scope.row)" >编辑</el-button>
            <el-button size="mini" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

    </div>
    </el-dialog>


    <!-- 添加或修改对话框 -->
    <el-dialog v-dialogDrag :close-on-click-modal="false" title="新增/编辑" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" label-position="top">
        <el-row :gutter="10">
          <FormItem label="分类标题" class="" :span="24" type="input" :form="form" prop="title" clearable />
          <FormItem label="排序" class="" :span="24" type="number" :form="form" prop="sort" clearable />
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open=false;">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')" :loading="is_ajax">{{is_ajax?'保存中···':'确定'}}</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import $ from "jquery";
// 上传组件
import SigleUpload from "@/components/UploadImage/sigleUpload";
export default {
  name: 'needCache',
  components: {SigleUpload},
  data () {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      activeName:'1',  // 1进行中 2 已结束
      loading: true,
      total: 0,
      // 定时任务表格数据
      list: [],
      // 查询参数
      queryParams: {
        page: 1,
        limit: 16,
        keywords: '',
        organization: '',
        role: ''
      },
      openOptions: [{id:'1',title:'选项1'},{id:'2',title:'选项2'}],
      gender_list: [{id:1,title:'男'},{id:2,title:'女'}],
      open:false,
      // 表单参数
      form: {
        title:''
      },
      // 表单校验
      rules: {
        title: [
          { required: true, message: "指标标题不能为空", trigger: "blur" }
        ]
      },
      is_ajax:false,
      group_list:[],
      unit_list:[],
      role_list:[],
      role_list2:[],
      pj_list:[],
      par_list:[],
      // 上传图片
      uploadImageUrl: '',
      uploadImgVisible: false,
      is_download:false,

      is_open_window:false,
      open2:false
    }
  },
  methods:{

    sw_change(e,row){
      const _this=this;
      row.is_pw=e;
      let _data={id:row.id,is_pw:e};
      let url="/api/SubjectCategory/set_pw_pf";
      _this.axios.post(url, _data).then(function(data) {
        if(data){
          _this.tip_show('操作成功','success');
          // _this.getList()
        }
      }).catch(function(error) {
        console.log(error);
      })

    },
    init(id){
      this.id=id;
      this.list=[];
      this.open2=true;
      this.getList()
    },

    // 表单重置
    reset() {
      if(this.$refs['form']){
        this.$refs['form'].clearValidate();
      }
    },
    /** 提交按钮 */
    submitForm: function(form) {
      const _this=this;
      this.$refs[form].validate(valid => {
        if (valid) {
          this.is_ajax=true;
          let _data={..._this[form]};
          let url="/api/SubjectCategory/save";
          _data.q_id=_this.id
          _this.axios.post(url, _data).then(function(data) {
            if(data){
              _this.tip_show('操作成功','success');
              _this.open=false;
              _this.getList()
            }
            _this.is_ajax=false;
          }).catch(function(error) {
            _this.is_ajax=false
            console.log(error);
          })
        }
      });
    },
    /** 查询定时任务列表 */
    getList() {
      const _this=this;
      this.loading = true;
      _this.axios.get("/api/SubjectCategory/index", {params:{q_id:this.id}}).then(function(res) {
        if(res){
          _this.list =res.message;
          _this.loading = false;
        }
      }).catch(function(error) {
        console.log(error);
      })
    },

    /** 修改按钮操作 */
    handleUpdate(row){
      this.reset()
      let form=row.id?{...row}:{
        title:'',
        q_id:this.id,
        desc:'',
        sort:0
      };
      this.form={...form}
      this.open=true;
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const _this=this;
      const ids = row.id;
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          _this.axios.post("/api/SubjectCategory/delete", {id:ids}).then((res) => {
              if (res) {
                  _this.getList();
                  _this.$message.success('操作成功');
              }
          }).catch((err) => {
              console.log(err);
          });
        })
    },

  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
